<?php
//
// 运费管理
//
class Controller_Admin_Deliver extends Controller_Form
{
    public function __construct ($req, $res)
    {
        $this->_form_type = 'deliver';
        parent::__construct($req, $res);

        $this->_list_where = array(
            array('status', '=',  1),
        );

        $this->_sort_extra_columns = array(
            array('changed', DB::expr('curdate()')),
        );

        $this->_shopId = App_Shop_Manager::currentShopId();
        if ($this->_shopId > 0)
            $this->_list_where[] = array('shopId', '=', $this->_shopId);
    }

	//默认首页
    public function action_index ()
    {
        $this->action_list();
    }

	//添加快递配置
    public function action_create ()
    {
        $post = $this->request->post();
        if (! empty($post))
        {
            $this->rebuild_post_data($post);
            $this->_form_data = $post;
        }

        parent::action_create();
    }

	//修改快递配置
    public function action_update ($nid)
    {
        $post = $this->request->post();
        if (! empty($post))
        {
            $this->rebuild_post_data($post);
            $this->_form_data = $post;
        }
        else
        {
            $data = Form_Data::detail($this->_form_type, $nid); 
            $this->_form_data = $data;
            $this->_form_data['money']       = number_format($this->_form_data['money']/100, 2);
            $this->_form_data['ltMoneyFee']  = number_format($this->_form_data['ltMoneyFee']/100, 2);
            $this->_form_data['egtMoneyFee'] = number_format($this->_form_data['egtMoneyFee']/100, 2);
        }

        parent::action_update($nid);
    }

	// 详情
	//$nid int 商铺ID
    public function action_detail ($nid)
    {
        $data = Form_Data::detail($this->_form_type, $nid); 
        $this->_form_data = $data;
        $this->_form_data['money']       = number_format($this->_form_data['money']/100, 2);
        $this->_form_data['ltMoneyFee']  = number_format($this->_form_data['ltMoneyFee']/100, 2);
        $this->_form_data['egtMoneyFee'] = number_format($this->_form_data['egtMoneyFee']/100, 2);

        parent::action_detail($nid);
    }

	// 删除快递配置
    public function action_delete ($nid)
    {
        App_Deliver::remove($nid);
        $this->to_url('list');
    }

    public function detail_filter_config_data (& $arr_config)
    {
        //
    }

	//重构数据
    private function rebuild_post_data (& $post)
    {
        //
        $post['ltMoneyFee']  = (int) ($post['ltMoneyFee'] * 100);
        $post['egtMoneyFee'] = (int) ($post['egtMoneyFee'] * 100);
        $post['money']       = (int) ($post['money'] * 100);
    }

	// 列表数据改造
    public function list_filter_data (& $rows)
    {
        foreach ($rows as & $rs)
        {
            //
            $shopId = $rs['shopId'];
            $rs['shopId'] = App_Shop::name($shopId);
            $rs['money']  = '￥'.number_format($rs['money']/100, 2);
            $rs['ltMoneyFee']  = '￥'.number_format($rs['ltMoneyFee']/100, 2);
            $rs['egtMoneyFee']  = '￥'.number_format($rs['egtMoneyFee']/100, 2);
        }
    }

	//操作菜单
    public function actions_by_list_row (& $row, & $menus)
    {
        //
    }
}
